package cn.tedu.kcircle.comment.front.infra.repository.persist;

import cn.tedu.kcircle.comment.dao.entity.CommentDO;
import cn.tedu.kcircle.comment.front.dao.mapper.CommentFrontMapper;
import cn.tedu.kcircle.comment.front.infra.repository.converter.CommentConverter;
import cn.tedu.kcircle.comment.front.protocol.param.CommentAddParam;
import cn.tedu.kcircle.comment.front.repository.persist.CommentRepository;
import cn.tedu.kcircle.comment.front.service.bo.CommentListItemBO;
import cn.tedu.kcircle.comment.front.service.bo.CommentStandardBO;
import cn.tedu.kcircle.common.dto.ResourceDTO;
import cn.tedu.kcircle.common.po.PageData;
import cn.tedu.kcircle.common.util.TransPage;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Slf4j
@Repository
@AllArgsConstructor
public class CommentRepositoryImpl implements CommentRepository {
    private CommentFrontMapper commentMapper;
    private CommentConverter converter;

    @Override
    public int insert(CommentAddParam param) {
        log.debug("开始执行【插入评论】的数据访问，参数：{}", param);
        CommentDO commentDO = converter.param2DO(param);
        return commentMapper.insert(commentDO);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除评论】的数据访问，参数：{}", id);
        return commentMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        log.debug("开始执行【批量删除评论】的数据访问，参数：{}", idList);
        return commentMapper.deleteBatchIds(idList);
    }

    @Override
    public int deleteByResource(ResourceDTO resourceDTO) {
        log.debug("开始执行【根据资源删除评论数据】的数据访问，参数：{}", resourceDTO);
        return commentMapper.deleteByResource(resourceDTO);
    }

    @Override
    public Long countByResource(ResourceDTO resourceDTO) {
        log.debug("开始执行【根据资源统计评论表中的数据的数量】的数据访问，参数：{}", resourceDTO);
        return commentMapper.selectCountByResource(resourceDTO);
    }

    @Override
    public CommentStandardBO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询评论信息】的数据访问，参数：{}", id);
        return commentMapper.getStandardById(id);
    }

    @Override
    public PageData<CommentListItemBO> listByResourceType(Integer resourceType, Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询评论列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<CommentListItemBO> list = commentMapper.listByResourceType(resourceType);
        PageHelper.clearPage();
        return TransPage.list2Page(list);
    }

    @Override
    public List<CommentListItemBO> listByAuthorId(Long authorId) {
        log.debug("开始执行【根据用户id查询用户所有评论】参数：{}",authorId);
        return commentMapper.listByAuthorId(authorId);
    }
}
